package com.study.zzedu.ui.yidonginfo;

import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.study.zzedu.R;
import com.study.zzedu.adapter.YiDongAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.YiDongModel;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class YiDongInfoActivity extends BaseMvpActivity<BaseView,YiDongInfoPresenter> implements BaseView{

    private RelativeLayout btnBack;
    List<YiDongModel> yiList = new ArrayList<>();
    RecyclerView rView;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_yi_dong_info;
    }

    @Override
    protected YiDongInfoPresenter createPresenter() {
        return new YiDongInfoPresenter();
    }

    @Override
    protected BaseView createView() {
        return this;
    }

    @Override
    protected void initView() {
        btnBack = findViewById(R.id.rl_back);
        rView = (RecyclerView)findViewById(R.id.rv_yidong);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rView.setLayoutManager(layoutManager);
    }

    @Override
    protected void initData() {
        getPresenter().getYiDongInfoList(YiDongInfoActivity.this);
    }

    @Override
    protected void initListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void getData(String data) {
        ToastUtil.showShortToast("获得异动List");
        yiList.clear();
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0;i<jArray.length();i++){
                yiList.add(new YiDongModel
                        (jArray.getJSONObject(i).getString("type"),
                         jArray.getJSONObject(i).getString("createDate"),
                        jArray.getJSONObject(i).getString("remark"),
                        "学习中心"+jArray.getJSONObject(i).getString("firstCheckStatus")+"-学院"+jArray.getJSONObject(i).getString("secondCheckStatus")
                ));
            }
            if (!(jArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }
        } catch (Exception e) {
            e.toString();
        }

        YiDongAdapter adapter = new YiDongAdapter(yiList);
        rView.addItemDecoration(new SpacesItemDecoration(30));
        rView.setAdapter(adapter);
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

}